package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.CollectBean;
import com.example.administrator.yuejian_2.fragment.ClubCourseFragment;
import com.example.administrator.yuejian_2.fragment.ClubIntroFragment;
import com.example.administrator.yuejian_2.fragment.ClubVideoFragment;
import com.example.administrator.yuejian_2.interservice.ClubService;
import com.example.administrator.yuejian_2.interservice.Const;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class ClubDetailActivity extends AppCompatActivity {
    ViewFlipper viewFlipper;
    private RadioGroup radioGroup_club;
    private int club_id;
    private int user_id;
    private String club_name;
    private TextView clubname;
    private Context context;
    private ImageButton collect_button;
    private ImageButton back;
    private int state;

    private ClubIntroFragment clubIntroFragment;
    private ClubCourseFragment clubCourseFragment;
    private ClubVideoFragment clubVideoFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fragmentManager = getSupportFragmentManager();
        setContentView(R.layout.activity_club_detail);
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
        club_id = getIntent().getIntExtra("club_id",-1);
        club_name = getIntent().getStringExtra("club_name");
        clubname= (TextView) findViewById(R.id.cname);
        clubname.setText(club_name);
        back = (ImageButton) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getState(user_id,club_id);
        collect_button = (ImageButton) findViewById(R.id.club_collect);
        collect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击收藏按钮 改变收藏状态
                getState(user_id,club_id);
                if (state == 1){
                    deleteCollect(user_id,club_id);
                    getState(user_id,club_id);
                }

                else {
                    addCollect(user_id,club_id);
                    getState(user_id,club_id);
                }
            }
        });
        radio();
    }
    private void getState(int u_id,int c_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<CollectBean> call = service.getstate(u_id,c_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    collect_button.setImageResource(R.drawable.ic_collected);
                    state = 1;

                }
                else {
                    collect_button.setImageResource(R.drawable.ic_collect);
                    state = 0;
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }
        });
    }

    private void addCollect(int u_id, int c_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<CollectBean> call = service.addcollect(u_id,c_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    Toast.makeText(ClubDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    collect_button.setImageResource(R.drawable.ic_collected);
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }


        });

    }

    private void deleteCollect(int u_id, int c_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<CollectBean> call = service.deletecollect(u_id,c_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    Toast.makeText(ClubDetailActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                    collect_button.setImageResource(R.drawable.ic_collect);
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }


        });

    }

    public void radio(){
        viewFlipper= (ViewFlipper) findViewById(R.id.flipper_club);
        viewFlipper.startFlipping();
        radioGroup_club = (RadioGroup) findViewById(R.id.radioGroup_clubdetail);
        radioGroup_club.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.intro:
                        clubIntroFragment = new ClubIntroFragment();
                        replaceFragment(clubIntroFragment);
                        break;
                    case R.id.course:
                        clubCourseFragment = new ClubCourseFragment();
                        replaceFragment(clubCourseFragment);
                        break;
                    case R.id.video:
                        clubVideoFragment = new ClubVideoFragment();
                        replaceFragment(clubVideoFragment);
                        break;

                }
            }
        });
        radioGroup_club.check(R.id.intro);

    }
    private void replaceFragment(Fragment fragment){
        fragmentManager= getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_holder_clubdetail,fragment);
        fragmentTransaction.commit();
    }




}
